﻿using System.Collections.Generic;
using System.Linq;
using BLL.Model;
using BLL.Model.Messaging;
using BLL.Model.Repositories;
using BLL.Model.Schema;

namespace BLL.ProjectManagement
{
    public class HeadManager
    {
        private IHeadRepository _headRepository;
        private readonly Message _latestMessage;
        public HeadManager(IHeadRepository headRepository)
        {
            _headRepository = headRepository;
            _latestMessage = new Message();
        }

        public IList<Head> GetHeads()
        {
            IList<Head> heads = _headRepository.GetAll();
            if (heads == null) return new List<Head>();
            return heads.Where(p => p.Name != "Cash Book" && p.Name != "Bank Book").ToList();
        }

        public bool AddOrUpdateHead(Head head)
        {
            Head existingHead = _headRepository.Get(head.Id);

            if (existingHead != null)
            {
                _headRepository.Update(head);
                _latestMessage.MessageText = "Account of head '" + head.Name + "' successfully updated"; //TODO: Change the string concatenation to string.format
                _latestMessage.MessageType = MessageType.Success;
            }
            else
            {
                Head insertedHead = _headRepository.Insert(head);
                _latestMessage.MessageText = "New Account of head '" + insertedHead.Name + "' successfully created"; //TODO: Change the string concatenation to string.format
                _latestMessage.MessageType = MessageType.Success;
            }
            return true; //TODO: change the return statement
        }
    }
}


